package com.ruoyi.workbench.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileTypeUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.common.utils.uuid.Snowflake;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysPostMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.workbench.domain.WorkFile;
import com.ruoyi.workbench.domain.WorkSpecializedClassManage;
import com.ruoyi.workbench.domain.WorkSpecializedClassManageUser;
import com.ruoyi.workbench.mapper.WorkFileMapper;
import com.ruoyi.workbench.mapper.WorkSpecializedClassManageMapper;
import com.ruoyi.workbench.mapper.WorkSpecializedClassManageUserMapper;
import com.ruoyi.workbench.service.IWorkSpecializedClassManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 专班管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-02
 */
@Service
public class WorkSpecializedClassManageServiceImpl implements IWorkSpecializedClassManageService
{
    @Autowired
    private WorkSpecializedClassManageMapper workSpecializedClassManageMapper;
    @Autowired
    private WorkSpecializedClassManageUserMapper workSpecializedClassManageUserMapper;

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private WorkFileMapper workFileMapper;
    @Autowired
    private SysPostMapper sysPostMapper;
    /**
     * 查询专班管理
     * 
     * @param sid 专班管理主键
     * @return 专班管理
     */
    @Override
    public WorkSpecializedClassManage selectWorkSpecializedClassManageBySid(Integer sid)
    {
        WorkSpecializedClassManage workSpecializedClassManage = workSpecializedClassManageMapper.selectWorkSpecializedClassManageBySid(sid);
        WorkSpecializedClassManage workSpecializedClassManage1 = getDeptAndPostByUserId(workSpecializedClassManage.getGroupLeaderId());
        workSpecializedClassManage.setGroupLeaderDeptName(workSpecializedClassManage1.getGroupLeaderDeptName());// 组长部门
        workSpecializedClassManage.setGroupLeaderPostName(workSpecializedClassManage1.getGroupLeaderPostName());// 组长职务
        List<WorkSpecializedClassManageUser> users = workSpecializedClassManageUserMapper.selectUserNameBySpecializedClassIdAndUserType(sid, "");
        for (WorkSpecializedClassManageUser user : users) {
            if (user.getUserId()==null){
                continue;
            }
            WorkSpecializedClassManage workSpecializedClassManage2 = getDeptAndPostByUserId(user.getUserId());
            user.setGroupLeaderDeptName(workSpecializedClassManage2.getGroupLeaderDeptName());
            user.setGroupLeaderPostName(workSpecializedClassManage2.getGroupLeaderPostName());
        }
        workSpecializedClassManage.setWorkSpecializedClassManageUserList(users);// 回显的 副职、成员
        workSpecializedClassManage.setWorkFiles(workFileMapper.selectAllByJoinClassId(workSpecializedClassManage.getSid()));// 附件
        return workSpecializedClassManage;
    }

    @Override
    public WorkSpecializedClassManage getDeptAndPostByUserId(Long userId) {
        SysUser sysUser = sysUserMapper.selectUserById(userId);
        SysDept sysDept = sysDeptMapper.selectDeptById(sysUser.getDeptId());
        String posts = sysPostMapper.selectAllByUserId(sysUser.getUserId()).stream().map(SysPost::getPostName).collect(Collectors.joining(","));
        WorkSpecializedClassManage workSpecializedClassManage = new WorkSpecializedClassManage();
        workSpecializedClassManage.setGroupLeaderDeptName(sysDept.getDeptName());
        workSpecializedClassManage.setGroupLeaderPostName(posts);
        return workSpecializedClassManage;
    }
    /**
     * 查询专班管理列表
     * 
     * @param workSpecializedClassManage 专班管理
     * @return 专班管理
     */
    @Override
    public List<WorkSpecializedClassManage> selectWorkSpecializedClassManageList(WorkSpecializedClassManage workSpecializedClassManage)
    {
        List<Integer> ids = workSpecializedClassManage.getIds();
        List<WorkSpecializedClassManage> workSpecializedClassManages = new ArrayList<>();
        if (ids != null && ids.size() > 0) {
            workSpecializedClassManages = workSpecializedClassManageMapper.selectAllBySidIn(ids);
        } else {
            workSpecializedClassManages  = workSpecializedClassManageMapper.selectWorkSpecializedClassManageList(workSpecializedClassManage);
        }
        for (WorkSpecializedClassManage specializedClassManage : workSpecializedClassManages) {
            specializedClassManage.setAssistantGroupLeaderNames(getUsers(specializedClassManage.getSid(),"1"));
            specializedClassManage.setMembers(getUsers(specializedClassManage.getSid(),"2"));
            specializedClassManage.setWorkFiles(workFileMapper.selectAllByJoinClassId(specializedClassManage.getSid()));
        }
        return workSpecializedClassManages;
    }

    private String getUsers(Integer sid,String userType) {
        String assistantGroupLeaderNames = workSpecializedClassManageUserMapper.
                selectUserNameBySpecializedClassIdAndUserType(sid, userType).stream().map(WorkSpecializedClassManageUser::getUserName).collect(Collectors.joining("，"));
        return assistantGroupLeaderNames;
    }

    /**
     * 新增专班管理
     * 
     * @param workSpecializedClassManage 专班管理
     * @return 结果
     */
    @Override
    public int insertWorkSpecializedClassManage(WorkSpecializedClassManage workSpecializedClassManage) {
        workSpecializedClassManage.setCreateTime(DateUtils.getNowDate());
        int i = workSpecializedClassManageMapper.insertWorkSpecializedClassManage(workSpecializedClassManage);
        // 添加附件表信息
        List<JSONObject> fjbs = workSpecializedClassManage.getFileList();
        for (JSONObject json : fjbs) {
            JSONObject fjInfo = json.getJSONObject("response");
            if (StringUtils.isNotNull(fjInfo)) {
                WorkFile workFile = new WorkFile();
                workFile.setFileUrl(fjInfo.getString("url"));
                workFile.setFilePath(fjInfo.getString("filePath"));
                String fileName = fjInfo.getString("originalFilename");
                workFile.setFileName(fileName);
                workFile.setFileType(FileTypeUtils.getFileType(fileName));
                workFile.setCreateTime(DateUtils.getNowDate());
                workFile.setJoinClassId(workSpecializedClassManage.getSid());
                workFileMapper.insertWorkFile(workFile);
            }
        }
        return i;
    }

    /**
     * 修改专班管理
     * 
     * @param workSpecializedClassManage 专班管理
     * @return 结果
     */
    @Override
    public int updateWorkSpecializedClassManage(WorkSpecializedClassManage workSpecializedClassManage)
    {
        Integer sid = workSpecializedClassManage.getSid();
        workSpecializedClassManage.setUpdateTime(DateUtils.getNowDate());
        List<WorkSpecializedClassManageUser> users = workSpecializedClassManage.getWorkSpecializedClassManageUserList();
        workSpecializedClassManageUserMapper.deleteBySpecializedClassId(workSpecializedClassManage.getSid());// 删除关联人员
        for (int i = 0; i < users.size(); i++) {// 新增关联人员
            if (users.get(i).getUserId()==null) {
                continue;
            }
            users.get(i).setSpecializedClassId(workSpecializedClassManage.getSid());
            users.get(i).setCreateTime(DateUtils.getNowDate());
            workSpecializedClassManageUserMapper.insertWorkSpecializedClassManageUser(users.get(i));
        }
        List<JSONObject> fjbs = workSpecializedClassManage.getFileList();
        workFileMapper.deleteByJoinClassId(sid);
        for (JSONObject json : fjbs) {
            JSONObject fjInfo = json.getJSONObject("response");
            if (StringUtils.isNotNull(fjInfo)) {
                WorkFile workFile = new WorkFile();
                workFile.setFileUrl(fjInfo.getString("url"));
                workFile.setFilePath(fjInfo.getString("filePath"));
                String fileName = fjInfo.getString("originalFilename");
                workFile.setFileName(fileName);
                workFile.setFileType(FileTypeUtils.getFileType(fileName));
                workFile.setCreateTime(DateUtils.getNowDate());
                workFile.setJoinClassId(sid);
                workFileMapper.insertWorkFile(workFile);
            } else {
                WorkFile workFile = new WorkFile();
                workFile.setFileUrl(json.getString("url"));
                workFile.setFilePath(json.getString("filePath"));
                String fileName = json.getString("name");
                workFile.setFileName(fileName);
                workFile.setFileType(FileTypeUtils.getFileType(fileName));
                workFile.setCreateTime(DateUtils.getNowDate());
                workFile.setJoinClassId(sid);
                workFileMapper.insertWorkFile(workFile);
            }
        }
        return workSpecializedClassManageMapper.updateWorkSpecializedClassManage(workSpecializedClassManage);
    }

    /**
     * 批量删除专班管理
     * 
     * @param sids 需要删除的专班管理主键
     * @return 结果
     */
    @Override
    public int deleteWorkSpecializedClassManageBySids(Integer[] sids)
    {
        List<Integer> idList = Arrays.asList(sids);
        workFileMapper.deleteByJoinClassIdIn(idList);// 删除关联附件
        workSpecializedClassManageUserMapper.deleteBySpecializedClassIdIn(idList);// 删除关联人员
        return workSpecializedClassManageMapper.deleteWorkSpecializedClassManageBySids(sids);
    }

    /**
     * 删除专班管理信息
     * 
     * @param sid 专班管理主键
     * @return 结果
     */
    @Override
    public int deleteWorkSpecializedClassManageBySid(Integer sid)
    {
        return workSpecializedClassManageMapper.deleteWorkSpecializedClassManageBySid(sid);
    }

    @Override
    public List<Map> getUser() {
        List<Map> userNameAndUserId = sysUserMapper.getUserNameAndUserId();
        return userNameAndUserId;
    }

}
